package com.example.testapp;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.content.res.Configuration;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private TextView helloTextView;
    private Button changeLangButton;
    private ImageView flagImageView;
    private int currentLang = 0; // 0:中文, 1:英文, 2:日文
    private int[] flagResources = {
            R.drawable.chineseflag,
            R.drawable.americanflag,
            R.drawable.frenchflag
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 创建主布局
        LinearLayout mainLayout = new LinearLayout(this);
        mainLayout.setOrientation(LinearLayout.VERTICAL);
        mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        ));
        mainLayout.setGravity(Gravity.CENTER);
        mainLayout.setPadding(30, 30, 30, 30);

        // 创建文本视图
        helloTextView = new TextView(this);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        textParams.setMargins(0, 0, 0, 40);
        helloTextView.setLayoutParams(textParams);
        helloTextView.setTextSize(24);
        helloTextView.setText(R.string.hello);

        // 创建国旗图片视图
        flagImageView = new ImageView(this);
        LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
                200, 120
        );
        imageParams.setMargins(0, 0, 0, 40);
        flagImageView.setLayoutParams(imageParams);
        flagImageView.setImageResource(flagResources[currentLang]);

        // 创建按钮
        changeLangButton = new Button(this);
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        changeLangButton.setLayoutParams(buttonParams);
        changeLangButton.setText(R.string.change_language);
        changeLangButton.setTextSize(18);

        // 设置按钮点击事件
        changeLangButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 切换到下一种语言
                currentLang = (currentLang + 1) % 3;
                changeLanguage();
            }
        });

        // 将所有视图添加到主布局
        mainLayout.addView(helloTextView);
        mainLayout.addView(flagImageView);
        mainLayout.addView(changeLangButton);

        // 设置主布局为内容视图
        setContentView(mainLayout);
    }

    // 切换语言的方法
    private void changeLanguage() {
        String languageCode;
        switch (currentLang) {
            case 0:
                languageCode = "zh"; // 中文
                break;
            case 1:
                languageCode = "en"; // 英文
                break;
            case 2:
                languageCode = "fr"; // 日文
                break;
            default:
                languageCode = "zh";
        }

        // 更新国旗显示
        flagImageView.setImageResource(flagResources[currentLang]);

        // 更改应用语言
        Locale locale = new Locale(languageCode);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getResources().updateConfiguration(config, getResources().getDisplayMetrics());

        // 更新界面文本
        helloTextView.setText(R.string.hello);
        changeLangButton.setText(R.string.change_language);
    }
}
